/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * EditPatientReasonPanel.java
 *
 * Created on Feb 15, 2012, 5:10:16 PM
 */
package ui.panels;

import entities.PatientMaterial;
import entities.ViewPatientMaterial;
import java.awt.Color;
import java.awt.Font;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.table.DefaultTableCellRenderer;
import manager.DoctorDriver;
import model.PatientMaterialTableModel;
import ui.windows.RemedySchedulerFrame;

/**
 *
 * @author viev
 */
public class EditPatientReasonPanel extends javax.swing.JPanel {

    /** Creates new form EditPatientReasonPanel */
    public EditPatientReasonPanel(RemedySchedulerFrame mainFrame) {
        this.mainFrame = mainFrame;
        driver = (DoctorDriver) mainFrame.getDriver();
        initComponents();
        includeModel();
    }

    private void includeModel() {
        model = new PatientMaterialTableModel(driver.showViewPatientMaterialByEID(mainFrame.getUser().getUserID()));
        materialTable.setModel(model);
        materialTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        materialTable.setFillsViewportHeight(true);
        materialTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
        materialTable.getTableHeader().setFont(new Font(null, Font.BOLD, 11));
        DefaultTableCellRenderer cellRenderer = new DefaultTableCellRenderer();
        cellRenderer.setBackground(Color.yellow);
        DefaultTableCellRenderer cellRenderer1 = new DefaultTableCellRenderer();
        cellRenderer1.setBackground(Color.red);
        cellRenderer1.setForeground(Color.white);
        DefaultTableCellRenderer cellRenderer2 = new DefaultTableCellRenderer();
        cellRenderer2.setBackground(Color.blue);
        cellRenderer2.setForeground(Color.white);
        materialTable.getColumnModel().getColumn(0).setPreferredWidth(150);
        materialTable.getColumnModel().getColumn(1).setPreferredWidth(100);
        materialTable.getColumnModel().getColumn(1).setCellRenderer(cellRenderer);
        materialTable.getColumnModel().getColumn(2).setPreferredWidth(70);        
        materialTable.getColumnModel().getColumn(2).setCellRenderer(cellRenderer1);
        materialTable.getColumnModel().getColumn(3).setPreferredWidth(70);        
        materialTable.getColumnModel().getColumn(3).setCellRenderer(cellRenderer2);
        materialTable.getColumnModel().getColumn(4).setPreferredWidth(350);

        lbCheckDate.setText("");
        checkDate = true;
        checkAssign = false;
        if(!model.isEmpty()) {
            selectedRecord = model.getAt(0);
            lbName.setText(selectedRecord.getPatientName());
            lbStartDate.setText(dateFormat.format(selectedRecord.getStartDate()));
            dcEndDate.setDate(selectedRecord.getEndDate());
            txaReason.setText(selectedRecord.getReason());
        }
    }

    private void setSelectedRecord() {
        int index = materialTable.getSelectedRow();
        if (index != -1) {
            selectedRecord = model.getAt(index);
            lbName.setText(selectedRecord.getPatientName());
            lbStartDate.setText(dateFormat.format(selectedRecord.getStartDate()));
            dcEndDate.setDate(selectedRecord.getEndDate());
            txaReason.setText(selectedRecord.getReason());
        } else {
            selectedRecord = null;
            lbName.setText("");
            lbStartDate.setText("");
            dcEndDate.setDate(null);
            txaReason.setText("");
        }
    }

    private void updateRecord() {
        if (selectedRecord != null) {
            checkDate();
            checkAssign();
            if (checkDate && checkAssign) {
                PatientMaterial record = new PatientMaterial();
                record.setEmployeeID(selectedRecord.getDoctorID());
                record.setPatientID(selectedRecord.getPatientID());
                record.setStartDate(selectedRecord.getStartDate());
                record.setEndDate(getOnlyDate(dcEndDate.getDate()));
                record.setReason(txaReason.getText());
                boolean returnVal = driver.updatePatientMaterial(record);
                if (returnVal) {
                    clear();
                    selectedRecord.setEndDate(record.getEndDate());
                    selectedRecord.setReason(record.getReason());
                    model.updateRecord(selectedRecord);
                    new Thread() {

                        @Override
                        public void run() {
                            JOptionPane.showMessageDialog(mainFrame, "Updated");
                        }
                    }.start();
                } else {
                    new Thread() {

                        @Override
                        public void run() {
                            JOptionPane.showMessageDialog(mainFrame, "Cannot update !", "Error", JOptionPane.WARNING_MESSAGE);
                        }
                    }.start();
                }
            }
        } else {
            JOptionPane.showMessageDialog(mainFrame, "You migh select one record !");
        }
    }

    private void checkDate() {
        Date endDate = getOnlyDate(dcEndDate.getDate());
        if (endDate != null) {
            if (endDate.before(selectedRecord.getStartDate())) {
                checkDate = false;
                lbCheckDate.setText("invalid !");
                lbCheckDate.setForeground(Color.white);
            } else {
                checkDate = true;
                lbCheckDate.setText("");
            }
        } else {
            checkDate = true;
            lbCheckDate.setText("");
        }
    }
    
    private void checkAssign() {
        String reason = txaReason.getText();
        Date endDate = getOnlyDate(dcEndDate.getDate());
        if(!reason.equals(selectedRecord.getReason()) || !endDate.equals(selectedRecord.getEndDate())) {
            checkAssign = true;
        } else {
            checkAssign = false;
            JOptionPane.showMessageDialog(mainFrame, "You have not update !");
        }
    }
    
    private Date getOnlyDate(Date date) {
        if(date != null) {
            return new Date(date.getYear(), date.getMonth(), date.getDate());
        }
        return null;
    }
    
    public void clear() {
        lbName.setText("");
        lbStartDate.setText("");
        dcEndDate.setDate(null);
        txaReason.setText("");
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jSplitPane1 = new javax.swing.JSplitPane();
        jScrollPane1 = new javax.swing.JScrollPane();
        materialTable = new javax.swing.JTable();
        jScrollPane2 = new javax.swing.JScrollPane();
        jPanel3 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        lbStartDate = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        dcEndDate = new com.toedter.calendar.JDateChooser();
        jLabel2 = new javax.swing.JLabel();
        jScrollPane3 = new javax.swing.JScrollPane();
        txaReason = new javax.swing.JTextArea();
        bUpdate = new javax.swing.JButton();
        lbCheckDate = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        lbName = new javax.swing.JLabel();

        setLayout(new java.awt.BorderLayout());

        jSplitPane1.setOrientation(javax.swing.JSplitPane.VERTICAL_SPLIT);

        jScrollPane1.setPreferredSize(new java.awt.Dimension(300, 102));

        materialTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        materialTable.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                materialTableMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(materialTable);

        jSplitPane1.setLeftComponent(jScrollPane1);

        jScrollPane2.setPreferredSize(new java.awt.Dimension(326, 235));

        jPanel3.setBackground(new java.awt.Color(0, 51, 51));

        jLabel1.setForeground(new java.awt.Color(204, 255, 204));
        jLabel1.setText("Start Date:");

        lbStartDate.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        lbStartDate.setForeground(new java.awt.Color(204, 255, 204));
        lbStartDate.setText("value");

        jLabel3.setForeground(new java.awt.Color(204, 255, 204));
        jLabel3.setText("End Date:");

        jLabel2.setForeground(new java.awt.Color(204, 255, 204));
        jLabel2.setText("Reason:");

        txaReason.setColumns(20);
        txaReason.setRows(5);
        jScrollPane3.setViewportView(txaReason);

        bUpdate.setText("Update");
        bUpdate.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bUpdateActionPerformed(evt);
            }
        });

        lbCheckDate.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        lbCheckDate.setForeground(new java.awt.Color(204, 255, 204));
        lbCheckDate.setText("value");

        jLabel4.setForeground(new java.awt.Color(204, 255, 204));
        jLabel4.setText("Name:");

        lbName.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        lbName.setForeground(new java.awt.Color(204, 255, 204));
        lbName.setText("value");

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPane3, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 602, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel3Layout.createSequentialGroup()
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel4)
                            .addComponent(jLabel1)
                            .addComponent(jLabel3))
                        .addGap(28, 28, 28)
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel3Layout.createSequentialGroup()
                                .addComponent(dcEndDate, javax.swing.GroupLayout.PREFERRED_SIZE, 120, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(lbCheckDate))
                            .addGroup(jPanel3Layout.createSequentialGroup()
                                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(lbStartDate)
                                    .addComponent(lbName))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 404, Short.MAX_VALUE)
                                .addComponent(bUpdate, javax.swing.GroupLayout.PREFERRED_SIZE, 84, javax.swing.GroupLayout.PREFERRED_SIZE))))
                    .addComponent(jLabel2, javax.swing.GroupLayout.Alignment.LEADING))
                .addContainerGap())
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(bUpdate)
                            .addGroup(jPanel3Layout.createSequentialGroup()
                                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(jLabel4)
                                    .addComponent(lbName))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(jLabel1)
                                    .addComponent(lbStartDate))))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel3)
                            .addComponent(dcEndDate, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addComponent(lbCheckDate))
                .addGap(18, 18, 18)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 171, Short.MAX_VALUE)
                .addContainerGap())
        );

        jScrollPane2.setViewportView(jPanel3);

        jSplitPane1.setRightComponent(jScrollPane2);

        add(jSplitPane1, java.awt.BorderLayout.CENTER);
    }// </editor-fold>//GEN-END:initComponents

private void materialTableMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_materialTableMouseClicked
    setSelectedRecord();
}//GEN-LAST:event_materialTableMouseClicked

private void bUpdateActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bUpdateActionPerformed
    updateRecord();
}//GEN-LAST:event_bUpdateActionPerformed
    private RemedySchedulerFrame mainFrame;
    private DoctorDriver driver;
    private PatientMaterialTableModel model;
    private ViewPatientMaterial selectedRecord;
    private boolean checkDate;
    private boolean checkAssign;
    private static SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/YYYY");
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton bUpdate;
    private com.toedter.calendar.JDateChooser dcEndDate;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JSplitPane jSplitPane1;
    private javax.swing.JLabel lbCheckDate;
    private javax.swing.JLabel lbName;
    private javax.swing.JLabel lbStartDate;
    private javax.swing.JTable materialTable;
    private javax.swing.JTextArea txaReason;
    // End of variables declaration//GEN-END:variables
}
